<?php

class SearchcasesController extends Controller_Base
{

    public function indexAction()
    {

        $tokenAuth = new TokenAuth('searchcase');
        SmartyAdapter::assign('token', $tokenAuth->access_token);
    }
    //案件查询
    public function searchAction(){
        $tokenAuth = new TokenAuth('searchcase');
        $token = $this->_request->getPost('token','');
        if (!$tokenAuth->isToken($token)) {
            AjaxTool::returnJsonData(400,'请刷新页面重新查询！');
        }
        $cardId = $this->_request->getPost('cardId','');
        if(empty($cardId)){
            AjaxTool::returnJsonData(401,'请输入您的证件号');
        }

        $name = $this->hasChinese($cardId);
        $last6CardId = '';
        if ($name) {
            $length = strlen($cardId); // 获取字符串长度
            $last6CardId = substr($cardId, $length - 6, 6); // 截取最后6位
            if(!Func_Common::checkCard6($last6CardId)){
                AjaxTool::returnJsonData(401,'请输入正确的身份证号或姓名+身份证后六位 列:张三123456');
            }
        } else {
            if(!Func_Common::checkCard($cardId)){
                AjaxTool::returnJsonData(401,'请输入正确的身份证号或姓名+身份证后六位 列:张三123456');
            }
        }
        //QPS验证
        $check_res = Lib_Tool::check_spam( 'search_case', $cardId);
        if ($check_res === 5502) {
            AjaxTool::returnJsonData(403,'操作太快啦，稍等一下');

        }
        if ($check_res === 5501) {
            AjaxTool::returnJsonData(403,'该身份证号码当日查询次数已超限');

        }

        if($last6CardId && $name) {
            $last6CardId = strtoupper($last6CardId);
            $wherestr =  "cardId LIKE '%{$last6CardId}' and litigantName = '{$name}'";
        }else{
            $cardId = strtoupper($cardId);
            $wherestr = "cardId = '{$cardId}'";
        }
        //公示案件库
        $publicCase =[];
        $publicList = Module_Mediate_PublicCase::getInstance()->getAllBySearch([],['where'=>["caseStatus = 1",$wherestr]],true);
        if($publicList){
            foreach ($publicList as $key => $val){
                $publicCase[$key]['litigantName'] = $val['litigantName'];
                $publicCase[$key]['cardId'] = strlen((string)$val['cardId']) == 6 ? $val['cardId'] :substr($val['cardId'] , 0 , 5) . '******' .substr($val['cardId'] , -4);
                $publicCase[$key]['disputeType'] = Module_Mediate_Case::$disputeType[$val['disputeType']];
                $publicCase[$key]['status'] = empty($val['mediateStatus']) ? '调解中' : Module_Mediate_PublicCase::$caseStatusArr[$val['mediateStatus']];
                $publicCase[$key]['mediationNum'] = empty($val['mediationNum']) ? "--" : $val['mediationNum'];
                $publicCase[$key]['assetsName'] = empty($val['assetsName']) ? "--" : $val['assetsName'];
                $publicCase[$key]['debtTotal'] = $val['debtTarget'];
            }
        }
        $sysList = Module_Mediate_Case::getInstance()->getAllBySearch(['litigantName,mediationNum,mediatorName,cardId,disputeType,debtTotal,sysStatus,assetsName'],['where'=>["sysStatus in (1,2)",$wherestr]]);
        $sysCase = [];
        if($sysList){
            foreach ($sysList as $key => $val){
                $sysCase[$key]['litigantName'] =$val['litigantName'];
                $sysCase[$key]['cardId'] = substr($val['cardId'] , 0 , 5) . '******' .substr($val['cardId'] , -4);
                $sysCase[$key]['disputeType'] = Module_Mediate_Case::$disputeType[$val['disputeType']];
                $sysCase[$key]['status'] = empty( $val['mediationNum']) ? "已受理" : ($val['sysStatus'] == 1 ? '调解中' : '已撤案');
                $sysCase[$key]['mediationNum'] = empty( $val['mediationNum']) ? "--" : $val['mediationNum'];
                $sysCase[$key]['assetsName'] = empty($val['assetsName']) ? "--" : $val['assetsName'];
                $sysCase[$key]['debtTotal'] = $val['debtTotal'];

            }
        }
        $cases = array_merge($sysCase,$publicCase);
        //查询案件
        if(empty($cases)){
            AjaxTool::returnJsonData(404,'未找到该身份证号下案件，请核实身份证号码是否正确');
        }
        $tokenAuth->dropToken($token);
        AjaxTool::returnJsonData(200,'success',$cases);

    }
    private function hasChinese($str) {
        preg_match('/[\x{4e00}-\x{9fa5}]+/u', $str, $matches);
        return empty($matches) ? $matches : $matches[0];

    }
}